<?php
vendor('utility', 'gersh_constants');
class Post extends AppModel {
	var $name = 'Post';
	var $validate = array(
		'user_id' => VALID_NOT_EMPTY,
		'title' => VALID_NOT_EMPTY
	);
	
	var $belongsTo = array(
			'User' =>
				array('className' => 'User',
						'foreignKey' => 'user_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'counterCache' => ''
				),

	);
	var $blog_settings = array(
		'siteurl' => 'http://www.gersh.no/',
		'blogname' => 'Gersh.no',
		'blogdescription' => "This is Gerhard Sletten's blog about webdev, cocoa, triathlon, food and life.",
		'admin_email' => 'gerhardsletten@gmail.com',
		'posts_per_rss' => 10,
		'blog_charset' => 'UTF-8',
		'ping_sites' => 'http://rpc.pingomatic.com/',
		'rss2_url' => 'http://www.gersh.no/rss'
		);
	
	// Utility functions used by Post and PostController
	function addReadMoreTag ($posts = array()) {
	 	$i = 0;
		if(!empty($posts)) {
		foreach($posts as $post) { 
			$readmore = strpos($post['Post']['body'], POST_EXCERPT);
			if ( $readmore === false ) {
				$posts[$i]['Post']['hasmore'] = false;
			} else {
				$posts[$i]['Post']['body'] = substr($post['Post']['body'], 0, $readmore);
				$posts[$i]['Post']['hasmore'] = true;
			}
			$readmore = false;
			$i++;
		}
		return $posts;
		} else return false;
	 }
	function getPostDescription($id) {
		$post = $this->read(null, $id);
		$readmore = strpos($post['Post']['body'], POST_EXCERPT);
		if ( $readmore === false ) {
			return $post['Post']['body'];
		} else {
			return substr($post['Post']['body'], 0, $readmore);
		}
	}
	
	function findDistinctTags() {
		//$posts = $this->findAll('Post.tag NOT LIKE ""', array('distinct `Post`.`tag`'), 'Post.tag DESC');
		$posts = $this->findAll('Post.tag NOT LIKE ""', array('DISTINCT `Post`.`tag`'), 'Post.tag DESC');
		$tags = array();
		foreach($posts as $post) {
			foreach($post['Post']['tags'] as $tag) {
				if (!in_array($tag, $tags)) $tags []= $tag;
			}
		}
		return $tags;
	}
	
	// Insert a url friendly Post.slug from the Post.title var, but only if the post is new (has no id yet)
	function beforeSave() { 
		if( isset($this->data['Post']['title']) and !$this->data['Post']['id']) 
			$this->data['Post']['slug'] = urlFriendlyString( $this->data['Post']['title'] ); 
		return parent::beforeSave(); 
	}
	
	// Adds Post.deleted to conditions to hide soft deleted post from result
	function beforeFind(&$queryData) {
		if (!is_array($queryData['conditions'])) {
			if($queryData['conditions'] == "") $queryData['conditions'] = 'Post.deleted != "1"';
			else $queryData['conditions'] = 'Post.deleted != 1 and ' . $queryData['conditions'];
			return $queryData;
		}
		return true;
		
	}

	// Add a non exiting DB row Post.url for easier to link to post in view
	// Modify Post.tag from string to an array
	function afterFind( $result ) { 
		$result = parent::afterFind( $result ); 
		foreach( $result as $k => $v ) { 
			if(isset($result[$k]['Post']['date']) and isset($result[$k]['Post']['slug'])) {
				$post_timestamp = mysqlToTimeStamp($result[$k]['Post']['date']); 
				$result[$k]['Post']['url'] = '/posts/archive/' . date('Y', $post_timestamp) . '/' . date('m', $post_timestamp) . '/' . $result[$k]['Post']['slug'];
			}
			if(isset($result[$k]['Post']['tag'])) {
				$tags = explode(" ", trim($result[$k]['Post']['tag']));
				$result[$k]['Post']['tags'] = array();
				foreach( $tags as $tag ) { 
					$result[$k]['Post']['tags'][] = $tag;
				}
			}
	  } 
	  return $result; 
	}
	
	// Sets Post.deleted to 1 instead of deleting Post
	function del($id = null, $cascade = true) {
		if ($id) {
			$this->id = $id;
			if ($this->exists() && $this->beforeDelete()) {
				if ($this->saveField('deleted', '1')) {
					$this->afterDelete();
					$this->_clearCache();
					$this->id = false;
					return true;
				}
			}
		}
		return false;
	}

}
?>